package com.zoe.vhr.controller.system.hr;

import cn.hutool.core.convert.Convert;
import com.zoe.vhr.model.Hr;
import com.zoe.vhr.model.Role;
import com.zoe.vhr.service.HrService;
import com.zoe.vhr.service.RoleService;
import com.zoe.vhr.util.R;
import com.zoe.vhr.utils.MultipartFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author Zoe.Zhang
 * @Date: 2021/08/08
 * @Description 操作员管理接口
 */
@RestController
@RequestMapping("/system/hr")
public class HRController {

    @Autowired
    HrService hrService;
    @Autowired
    RoleService roleService;

    @GetMapping("/listAllHrs")
    public R listAllHrs(Hr hr) {
        List<Hr> hrs = hrService.listAllHrs(hr);
        return R.success(hrs);
    }

    @PutMapping("/updateHr")
    public R updateHr(@RequestBody Hr hr) {
        int count = hrService.updateHr(hr);
        if (count > 0) {
            return R.success("修改成功");
        }
        return R.failed("修改失败");
    }

    @GetMapping("/listAllRoles")
    public R listAllRoles() {
        List<Role> roles = roleService.listAllRoles();
        return R.success(roles);
    }

    @PutMapping("/updateHrWithRole")
    public R updateHrWithRole(@RequestBody Map<String, Object> map) {
        Integer hrId = Convert.toInt(map.get("hrId"));
        Integer[] roleIds = Convert.toIntArray(map.get("roleIds"));
        int count = hrService.updateHrWithRole(hrId, roleIds);
        return R.success("修改成功");
    }

    @PostMapping("/saveHr")
    public R saveHr(HttpServletRequest request, Hr hr) {
        MultipartFile multipartFile = MultipartFileUtil.getSingleFile(request);
        int i = hrService.saveHr(hr, multipartFile);
        if (i > 0) {
            return R.success("添加成功");
        }
        return R.failed("添加失败");
    }
}
